#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

int stack_creat(Stack *stack)
{
	if(NULL == stack)
	{
		fprintf(stderr,"stack is NULL\n");
		return -1;
	}

	stack->pTop = (Node *)malloc(sizeof(Node));
	if(NULL == stack->pTop)
	{
		fprintf(stderr,"malloc is error\n");
		return -1;
	}
	stack->pTop->pNext = NULL;
	stack->pBottom =stack->pTop; 
	return 0;
}

int push_stack(Stack *stack ,char data)
{
	Node *pNew;
	if(NULL == stack)
	{
		fprintf(stderr,"stack is NULL\n");
		return -1;
	}
	pNew = (Node *)malloc(sizeof(Node));
	pNew->data = data;
	pNew->pNext = stack->pTop;
	stack->pTop = pNew;
	return 0;
}


int pop_stack(Stack *stack ,char *data)
{
	Node *pTemp;
	if(NULL == stack)
	{
		fprintf(stderr,"stack is NULL\n");
		return -1;
	}
	*data = stack->pTop->data;
	pTemp = stack->pTop;
	stack->pTop = pTemp->pNext;
	free(pTemp);
	pTemp = NULL;
	return 0;
}

int show_stack(Stack *stack)
{
	Node *pTemp;
	if(NULL == stack)
	{
		fprintf(stderr,"stack is NULL\n");
		return -1;
	}
	if(empty_stack(stack))
    {
        fprintf(stderr, "stack is empty\n");
        return -1;
    }

	pTemp = stack->pTop;
	while(pTemp !=stack->pBottom)
	{
		printf("%c ",pTemp->data);
		pTemp = pTemp->pNext;
	}
	puts("");
	return 0;
}


int empty_stack(Stack *stack)
{
    if(NULL == stack)
    {
        fprintf(stderr, "stack is NULL\n");
        return -1;
    }
    if(stack->pTop == stack->pBottom)
        return 1;
    else
        return 0;

}

char Top_stack(Stack *stack)
{
	return stack->pTop->data;
}

void end_node(Stack *stack)
{
	Node *pTemp;
	int i = 0;
	pTemp = stack->pTop;
	while(pTemp !=stack->pBottom)
	{
		endnum[i++] = pTemp->data;
		 
		pTemp = pTemp->pNext;
	}
	endnum[i] = '\0';

}

int stack_end_num(Stack *stack)
{
	Node *pTemp;
	int i = 0 ,n = 0;
	pTemp = stack->pTop;
	while(pTemp !=stack->pBottom)
	{
		 n++;
		pTemp = pTemp->pNext;
	}
	return n;
}